package com.education.welco.service.impl;

import com.education.common.pojo.AjaxResult;
import com.education.common.utils.Convert;
import com.education.welco.mapper.SAnsweringCommentMapper;
import com.education.welco.mapper.SAnsweringLikeMapper;
import com.education.welco.pojo.SAnsweringComment;
import com.education.welco.pojo.SAnsweringLike;
import com.education.welco.service.ISAnsweringLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 课堂答疑用户点赞 服务层实现
 *
 * @author d
 * @date 2022-06-25
 */
@Service
public class SAnsweringLikeServiceImpl implements ISAnsweringLikeService {

    @Autowired
    private SAnsweringLikeMapper sAnsweringLikeMapper;
    @Autowired
    private SAnsweringCommentMapper sAnsweringCommentMapper;

    /**
     * 新增(课程答疑评论相关点赞)
     * @param userId 用户id
     * @param userName 用户名称
     * @param userIco 用户头像
     * @param likeId 点赞资源id
     * @param type 类型 1、问答点赞 2、动态点赞、3、话题点赞，4、文章点赞 5、评论点赞
     * @return 结果
     */
    @Override
    public AjaxResult updateSysUserForumsLikeItData(String userId, String userName, String userIco, String likeId, String type) {
        //点赞数
        int i = 1;
        //查询是否有该条数据的存在 进行数据的操作
        SAnsweringLike selectLikeIt = new SAnsweringLike();
        selectLikeIt.setCLikeitId(likeId);
        selectLikeIt.setCUserId(userId);
        selectLikeIt.setCKeyType(type);
        List<SAnsweringLike> list = sAnsweringLikeMapper.selectSysUserforumsLikeitList(selectLikeIt);
        if(list.size() > 0){
            SAnsweringLike userforumsLikeit = list.get(0);
            Integer state = userforumsLikeit.getNLikeitState();
            if(state == 0){
                //未点赞 进行点赞
                selectLikeIt.setNLikeitState(1);
            }else {
                //已点赞 取消点赞
                i = -1;
                selectLikeIt.setNLikeitState(0);
            }
            //修改这条数据的点赞状态
            sAnsweringLikeMapper.updateSysUserforumsLikeit(selectLikeIt);
        }else {
            //该用户未对此资源进行操作  添加记录
            SAnsweringLike likeIt = new SAnsweringLike();
            likeIt.setCLikeitId(likeId);
            likeIt.setCKeyType(type);
            likeIt.setTCreateTime(new Date());
            likeIt.setCUserId(userId);
            likeIt.setCUserIco(userIco);
            likeIt.setCUserName(userName);
            likeIt. setNLikeitState(1);
            likeIt.setCCreateBy(userIco);
            sAnsweringLikeMapper.insertSysUserforumsLikeit(likeIt);
        }

        HashMap<String, String> resMap = new HashMap<>();
        if("5".equals(type)){
            //修改该评论的点赞数
            SAnsweringComment sysUserforumsComment = new SAnsweringComment();
            sysUserforumsComment.setCPlId(Integer.valueOf(likeId));
            sysUserforumsComment.setNLikeNum(i);
            sAnsweringCommentMapper.updateSysUserforumsComment(sysUserforumsComment);
            //拿到最新的点赞数量
            SAnsweringComment sAnsweringComment = sAnsweringCommentMapper.selectSAnsweringCommentById(Integer.valueOf(likeId));
            resMap.put("likeResult",String.valueOf(i));
            resMap.put("likeNum",String.valueOf(sAnsweringComment.getNLikeNum()));
        }

        //-1 未点赞    1 已点赞
        return AjaxResult.success("操作成功", resMap);
    }

    /**
     * 查询课堂答疑用户点赞信息
     *
     * @param cLikeitId 课堂答疑用户点赞ID
     * @return 课堂答疑用户点赞信息
     */
    @Override
    public SAnsweringLike selectSAnsweringLikeById(String cLikeitId) {
        return sAnsweringLikeMapper.selectSAnsweringLikeById(cLikeitId);
    }

    /**
     * 查询课堂答疑用户点赞列表
     *
     * @param sAnsweringLike 课堂答疑用户点赞信息
     * @return 课堂答疑用户点赞集合
     */
    @Override
    public List<SAnsweringLike> selectSAnsweringLikeList(SAnsweringLike sAnsweringLike) {
        return sAnsweringLikeMapper.selectSAnsweringLikeList(sAnsweringLike);
    }

    /**
     * 新增课堂答疑用户点赞
     *
     * @param sAnsweringLike 课堂答疑用户点赞信息
     * @return 结果
     */
    @Override
    public int insertSAnsweringLike(SAnsweringLike sAnsweringLike) {
        return sAnsweringLikeMapper.insertSAnsweringLike(sAnsweringLike);
    }

    /**
     * 修改课堂答疑用户点赞
     *
     * @param sAnsweringLike 课堂答疑用户点赞信息
     * @return 结果
     */
    @Override
    public int updateSAnsweringLike(SAnsweringLike sAnsweringLike) {
        return sAnsweringLikeMapper.updateSAnsweringLike(sAnsweringLike);
    }

    /**
     * 删除课堂答疑用户点赞对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSAnsweringLikeByIds(String ids) {
        return sAnsweringLikeMapper.deleteSAnsweringLikeByIds(Convert.toStrArray(ids));
    }
}